﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

namespace Workflows.Components.TextMining.TextScraping
{
    /// <summary>
    /// 
    /// </summary>
    public class HeaderFilter:ContentFilterBase 
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public HeaderFilter(Guid id) : base(id)
        {
        }

        #region Overrides of ContentFilterBase

        public override event ContentFiltered OnContentFiltered;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="srcContent"></param>
        public override void Filter(object srcContent)
        {
            Debug.Assert(srcContent !=null && srcContent.GetType()==typeof(HeaderContentProvider));
            HeaderContentProvider provider = (HeaderContentProvider) srcContent;
            if(Regex.IsMatch(provider.SrcContent,provider.Pattern, RegexOptions.IgnoreCase))
            {
                Match match = Regex.Match(provider.SrcContent, provider.Pattern, RegexOptions.IgnoreCase);
                string content =
                    provider.SrcContent.Substring(match.Index + match.Value.Length);
                if (this.OnContentFiltered != null)
                    this.OnContentFiltered(this.FilterID, content);
            }
            else
            {
                if (this.OnContentFiltered != null)
                    this.OnContentFiltered(this.FilterID, provider.SrcContent);
            }
        }

        #endregion
    }
}
